package service

import (
	"boy-go/modules/demo/model"
	"boy-go/modules/demo/repository"
	"boy-go/modules/demo/vo"
	"boy-go/pkg/request"
	"github.com/jinzhu/copier"
)

func NewTCompanyService() TCompanyService {
	return &tCompanyService{
		TCompanyRepo: repository.NewTCompanyRepository(),
	}
}

type tCompanyService struct {
	TCompanyRepo *repository.TCompanyRepository
}

// GetById 根据id获取配置信息
func (s *tCompanyService) GetById(id int64) (*vo.TCompanyModel, error) {
	m, err := s.TCompanyRepo.GetById(id)
	if err != nil {
		return nil, err
	}
	rsp := new(vo.TCompanyModel)
	if err := copier.Copy(rsp, m); err != nil {
		return nil, err
	}
	return rsp, nil
}

// List 获取演示公司表列表
func (s *tCompanyService) List(req *vo.QueryTCompanyReq, pageReq *request.PageRequest) (*vo.ListTCompanyRsp, error) {
	list, total, err := s.TCompanyRepo.List(req, pageReq)
	if err != nil {
		return nil, err
	}
	itemList := make([]*vo.TCompanyModel, 0)
	for _, v := range list {
		item := new(vo.TCompanyModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return &vo.ListTCompanyRsp{
		List:  itemList,
		Total: total,
	}, nil
}

// Add 添加演示公司表
func (s *tCompanyService) Add(req *vo.AddTCompanyReq) (*vo.AddTCompanyRsp, error) {
	m := new(model.TCompany)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.TCompanyRepo.Save(m); err != nil {
		return nil, err
	}
	return &vo.AddTCompanyRsp{
		Id: m.Id,
	}, nil
}

// Edit 修改演示公司表
func (s *tCompanyService) Edit(req *vo.EditTCompanyReq) (*vo.EditTCompanyRsp, error) {
	m := new(model.TCompany)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.TCompanyRepo.Update(m); err != nil {
		return nil, err
	}
	return &vo.EditTCompanyRsp{
		Id: m.Id,
	}, nil
}

// Del 删除演示公司表
func (s *tCompanyService) Del(id int64) error {
	return s.TCompanyRepo.Delete(id)
}

func (s *tCompanyService) BatchDel(ids []string) error {
	return s.TCompanyRepo.BatchDelete(ids)
}
